home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Why does this work?
- Date: 25 Feb 1996 12:16:00 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gqg20INN21s@keats.ugrad.cs.ubc.ca>
- References: <4g8f7o$adt@ncar.ucar.edu> <4geg2t$k3f@news.cencom.net>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4geg2t$k3f@news.cencom.net>, Bill Wendling <tanp@ns> wrote:
- >Jim Rosinski inexplicably wrote:
- >
- >} Could someone please explain why the following code works? In particular, I
- >} am perplexed as to why "sub1" and "sub2" declared as elements of "cmndtable"
- >} are valid "pointers to function returning void" (i.e. see the definition of
- >} "cmndstruct"). When executed, the net result is indeed to invoke two
- >} functions, "sub1", and "sub2" (tested on Solaris, AIX, and UNICOS).
- >
- >} #include <stdio.h>
- >} main()
- >} {
- >} void sub1(), sub2();
- >
- >} struct cmndstruct {
- >} void (*funcnam)();
- >} };
- >
- >} struct cmndstruct cmndtable[] = {
- >} sub1,
- >} sub2,
- >} NULL
- >} };
- >/*
- >sub1 and sub2 are the reference to the functions that they call. This has
- >the same effect of doing:
- >
- >void sub1();
- >void (*funcnam)();
- >
- >funcnam = sub1;
- >
- >This is exactly how you assign a function to a function pointer.
-
- You missed the point. He obviously wanted to know why his functions are being
- called, depite that he has omitted parentheses from the aggregate initializers,
- which place the sub1, and sub2 above on the same nesting level as structure
- initializers. It appears as though sub1 and sub2 can be directly (and
- incorrectly) assigned as members of a array of structures.
-
- This is a feature of ANSI C. In aggregate initializers, braces may be elided,
- in which case the initializer elements are handed to whatever fields are
- available in the aggregate variable in sequence. Thus, you can do:
-
- int myarray[3][3][3] = { 1, 2, 3, 4, 5, 6, 7, 8 };
-
- Instead of several levels of bracket nesting.
-
- --
-
-